package com.atguigu.product.service.impl;

import com.atguigu.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.product.dao.AttrGroupDao;
import com.atguigu.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.product.entity.AttrEntity;
import com.atguigu.product.entity.AttrGroupEntity;
import com.atguigu.product.service.AttrAttrgroupRelationService;
import com.atguigu.product.service.AttrGroupService;
import com.atguigu.product.service.AttrService;
import com.atguigu.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.product.vo.AttrRelationVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao AttrAttrgroupRelationDao;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils PageCatIdAndKey(Map<String, Object> params,Long catId) {
        String key=(String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        //            指定catId时，如果有查询条件(key),就进行查询，key可能是attr_group_id，也可能是attr_group_name包含的数据
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
//        不指定catId时查询全部
        if(catId==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params)
                    , queryWrapper);
            PageUtils pageUtils = new PageUtils(page);
            return pageUtils;
        }else {
            queryWrapper.eq("catelog_id",catId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            PageUtils pageUtils = new PageUtils(page);
            return pageUtils;
        }
    }

//    删除关联关系（删除AttrAttrGroup表中的数据）
    @Override
    public void removeRelation(AttrRelationVo[] attrRelationVos) {
//        这里需要删除的都是关系表中的数据，所以先将传入的对象转换成关系表的实体类
//        然后删除
        List<AttrAttrgroupRelationEntity> attrAttrGroup = Arrays.asList(attrRelationVos).stream()
                .map((item) -> {
                    AttrAttrgroupRelationEntity attrAttrgroupRelation = new AttrAttrgroupRelationEntity();
                    BeanUtils.copyProperties(item, attrAttrgroupRelation);
                    return attrAttrgroupRelation;
                }).collect(Collectors.toList());


//        因为删除的是关系表，所以将关系表的Dao引入进来，然后进行删除
//        这里使用Dao，在mybatis的xml文件里写sql语句
        AttrAttrgroupRelationDao.deleteRelation(attrAttrGroup);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //       1  查出当前分类下的所有分组
        List<AttrGroupEntity> groupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
//       2  查出分组下的所有属性
        List<AttrGroupWithAttrsVo> vos = groupEntityList.stream()
                .map(item -> {
                    AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
                    BeanUtils.copyProperties(item, vo);
                    List<AttrEntity> attrEntityList = attrService.groupIdGetAttr(vo.getAttrGroupId());
                    vo.setAttrs(attrEntityList);
                    return vo;
                }).collect(Collectors.toList());
        return vos;
    }
}